package com.insta.java.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.insta.java.model.Location;

@Repository("locationDao")
public class LocationDao implements ILocationDao{

	private SessionFactory sessionFactory;
	
	@Autowired
	public LocationDao(SessionFactory sessionFactory){
		super();
		this.sessionFactory= sessionFactory;
	}
	
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	public void closeSession(){
		sessionFactory.close();
	}
	
	/* (non-Javadoc)
	 * @see com.insta.java.dao.ILocationDao#save(com.insta.java.model.Location)
	 */
	public void save(Location location) {
		Session session= getSession();
		Transaction tx=null;
		try{
			tx= session.beginTransaction();
			session.save(location);
			tx.commit();
		}catch (RuntimeException e) {
			if(tx!=null){
				tx.rollback();
			}
			throw e;
		}
	}

	/* (non-Javadoc)
	 * @see com.insta.java.dao.ILocationDao#update(com.insta.java.model.Location)
	 */
	public void update(Location location) {
		Session session= getSession();
		Transaction tx=null;
		try{
			tx= session.beginTransaction();
			session.update(location);
			tx.commit();
		}catch (RuntimeException e) {
			if(tx!=null){
				tx.rollback();
			}
			throw e;
		}
		
	}

	/* (non-Javadoc)
	 * @see com.insta.java.dao.ILocationDao#delete(com.insta.java.model.Location)
	 */
	public void delete(Location location) {
		Session session= getSession();
		Transaction tx=null;
		try{
			tx= session.beginTransaction();
			session.delete(location);
			tx.commit();
		}catch (RuntimeException e) {
			if(tx!=null){
				tx.rollback();
			}
			throw e;
		}
	}

	/* (non-Javadoc)
	 * @see com.insta.java.dao.ILocationDao#findByUserLogin(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public List<Location> findByUserId(Integer id) {
		Session session = getSession();
		Transaction tx=null;
		try{
			tx=session.beginTransaction();
			Query query = session.createQuery("from Location where user_id=?");
			query.setInteger(0, id);
			List<Location> list = query.list();
			tx.commit();
			return list;
		}catch (RuntimeException e) {
			if(tx!=null){
				tx.rollback();
			}
			throw e;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.insta.java.dao.ILocationDao#findByBookIsbn(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public List<Location> findByBookIsbn(String isbn) {
		Session session = getSession();
		Transaction tx=null;
		try{
			tx=session.beginTransaction();
			Query query = session.createQuery("from Location where bookISBN=?");
			query.setString(0, isbn);
			List<Location> list = query.list();
			tx.commit();
			return list;
		}catch (RuntimeException e) {
			if(tx!=null){
				tx.rollback();
			}
			throw e;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.insta.java.dao.ILocationDao#findById(java.lang.Integer)
	 */
	public Location findById(Integer id) {
		Session session = getSession();
		Transaction tx=null;
		try{
			tx=session.beginTransaction();
			Criteria criteria= session.createCriteria(Location.class)
					.add(Restrictions.idEq(id));

			Location temp= (Location) criteria.uniqueResult();
			tx.commit();
			return temp;
		}catch (RuntimeException e) {
			if(tx!=null){
				tx.rollback();
			}
			throw e;
		}
	}

}
